package com.systar.activity.events;

import java.nio.ByteBuffer;


public class ParsingActivityEvent extends AbstractActivityEvent
{
	public static final String TYPE = "parsing";

	private int nbInfo = 0;
	private int nbWarn = 0;
	private int nbErr = 0;
	private String errMessage;

	public ParsingActivityEvent(int nbInfo, int nbWarn, int nbErr, String errMessage)
	{
		this.nbInfo = nbInfo;
		this.nbWarn = nbWarn;
		this.nbErr = nbErr;
		this.errMessage = errMessage;
	}

	public int getNbInfo()
	{
		return nbInfo;
	}

	public int getNbWarn()
	{
		return nbWarn;
	}

	public int getNbErr()
	{
		return nbErr;
	}

	/**
	 * Concat the other information to this
	 * 
	 * @param other
	 *            other ParsingActivityEvent
	 */
	@Override
	public void concat(IActivityEvent otherEvent)
	{
		if (!getType().equals(otherEvent.getType()))
			return;
		
		ParsingActivityEvent other = (ParsingActivityEvent)otherEvent;
		this.nbInfo += other.nbInfo;
		this.nbWarn += other.nbWarn;
		this.nbErr += other.nbErr;
		if (this.errMessage == null)
		{
			this.errMessage = other.errMessage;
		}
		else if (other.errMessage != null)
		{
			this.errMessage += "\r\n" + other.errMessage;
		}
	}

	@Override
	public String getType()
	{
		return TYPE;
	}

	@Override
	public String getMessage()
	{
		return errMessage;
	}

	@Override
	public String marshal()
	{
		if (nbErr == 0)
			return null;
		return super.marshal() + nbErr;
	}

	@Override
	public void marshal(ByteBuffer data)
	{
		int storedNbErr = data.getInt(1);
		storedNbErr += nbErr;
		data.putInt(1, storedNbErr);
	}
}
